package com.solr.product.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;

import com.solr.product.bean.Price;
import com.solr.product.dao.BaseDao;
import com.solr.product.dao.PriceDao;


public class PriceDaoImpl extends BaseDao implements PriceDao{
	

	public void add(Price price) {
		String sql = "insert into t_price (productid,type,value) values (?,?,?)";
		List<Object> params = new ArrayList<Object>();
		params.add(price.getProductid());
		params.add(price.getType());
		params.add(price.getValue());
		getIndexDBTemplate().update(sql, params.toArray());
		
	}

	public Price getPriceByProductid(int productid) {
		String sql = "select * from t_price where productid = ?";
		List<Object> params = new ArrayList<Object>();
		params.add(productid);
		Price price = getIndexDBTemplate().queryForObject(sql, params.toArray(),new RowMapper<Price>(){

			public Price mapRow(ResultSet rs, int rowNum) throws SQLException {
				Price price = new Price();
				price.setId(rs.getInt("id"));
				price.setProductid(rs.getInt("productid"));
				price.setType(rs.getInt("type"));
				price.setValue(rs.getInt("value"));
				return price;
			}
			
		});
		return price;
	}

}
